// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/functions/prestosql/aggregates/bitwise_aggregates.h>
#include <pollux/functions/lib/aggregates/bitwise_aggregate_base.h>
#include <pollux/functions/prestosql/aggregates/aggregate_names.h>

using namespace kumo::pollux::functions::aggregate;

namespace kumo::pollux::aggregate::prestosql {
    namespace {
        template<typename T>
        class BitwiseOrAggregate : public BitwiseAggregateBase<T> {
        public:
            explicit BitwiseOrAggregate(TypePtr resultType)
                : BitwiseAggregateBase<T>(
                    resultType,
                    /* initialValue = */ 0) {
            }

            void addRawInput(
                char **groups,
                const SelectivityVector &rows,
                const std::vector<VectorPtr> &args,
                bool mayPushdown) override {
                SimpleNumericAggregate<T, T, T>::template updateGroups<true>(
                    groups,
                    rows,
                    args[0],
                    [](T &result, T value) { result |= value; },
                    mayPushdown);
            }

            void addSingleGroupRawInput(
                char *group,
                const SelectivityVector &rows,
                const std::vector<VectorPtr> &args,
                bool mayPushdown) override {
                SimpleNumericAggregate<T, T, T>::updateOneGroup(
                    group,
                    rows,
                    args[0],
                    [](T &result, T value) { result |= value; },
                    [](T &result, T value, int /* unused */
            ) {
                        result |= value;
                    },
                    mayPushdown,
                    this->initialValue_);
            }
        };

        template<typename T>
        class BitwiseAndAggregate : public BitwiseAggregateBase<T> {
        public:
            explicit BitwiseAndAggregate(TypePtr resultType)
                : BitwiseAggregateBase<T>(
                    resultType,
                    /* initialValue = */ -1) {
            }

            void addRawInput(
                char **groups,
                const SelectivityVector &rows,
                const std::vector<VectorPtr> &args,
                bool mayPushdown) override {
                SimpleNumericAggregate<T, T, T>::template updateGroups<true>(
                    groups,
                    rows,
                    args[0],
                    [](T &result, T value) { result &= value; },
                    mayPushdown);
            }

            void addSingleGroupRawInput(
                char *group,
                const SelectivityVector &rows,
                const std::vector<VectorPtr> &args,
                bool mayPushdown) override {
                SimpleNumericAggregate<T, T, T>::updateOneGroup(
                    group,
                    rows,
                    args[0],
                    [](T &result, T value) { result &= value; },
                    [](T &result, T value, int /* unused */
            ) {
                        result &= value;
                    },
                    mayPushdown,
                    this->initialValue_);
            }
        };
    } // namespace

    void registerBitwiseAggregates(
        const std::string &prefix,
        bool withCompanionFunctions,
        bool onlyPrestoSignatures,
        bool overwrite) {
        registerBitwise<BitwiseOrAggregate>(
            prefix + kBitwiseOr,
            withCompanionFunctions,
            onlyPrestoSignatures,
            overwrite);
        registerBitwise<BitwiseAndAggregate>(
            prefix + kBitwiseAnd,
            withCompanionFunctions,
            onlyPrestoSignatures,
            overwrite);
    }
} // namespace kumo::pollux::aggregate::prestosql
